//package com.cyn.security;
//
//import com.cyn.entity.system.RoleDO;
//import com.cyn.entity.system.UserDO;
//import com.cyn.service.MenuService;
//import com.cyn.service.RoleService;
//import com.cyn.service.UserService;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.security.core.GrantedAuthority;
//import org.springframework.security.core.authority.SimpleGrantedAuthority;
//import org.springframework.security.core.userdetails.User;
//import org.springframework.security.core.userdetails.UserDetails;
//import org.springframework.security.core.userdetails.UserDetailsService;
//import org.springframework.security.core.userdetails.UsernameNotFoundException;
//import org.springframework.stereotype.Service;
//
//import java.util.ArrayList;
//import java.util.List;
//import java.util.Set;
//
//@Service
//public class MyUserDetailsService implements UserDetailsService {
//
//
//    @Autowired
//    UserService userService;
//
//    @Autowired
//    RoleService roleService;
//
//    @Autowired
//    MenuService menuService;
//
//    /**
//     * 授权的时候是对角色授权，而认证的时候应该基于资源，而不是角色，因为资源是不变的，而用户的角色是会变的
//     */
//    @Override
//    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
//        UserDO userDO = userService.getUserByName(username);
//        if (null == userDO) {
//            throw new UsernameNotFoundException(username);
//        }
//
//        List<RoleDO> roleDOList = roleService.list(userDO.getUserId());
//
//        Set<String> listPerms = menuService.listPerms(userDO.getUserId());
//
//        List<GrantedAuthority> authorities = new ArrayList<>();
//        for (String listPerm : listPerms) {
//            authorities.add(new SimpleGrantedAuthority(listPerm));
//        }
//
//        return new User(userDO.getUsername(), userDO.getPassword(), authorities);
//    }
//}
